<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:include page="admin_header.jsp">
    <jsp:param name="title" value="分类管理"/>
    <jsp:param name="active" value="categories"/>
</jsp:include>

<!-- 主内容区 -->
<main role="main" class="col-md-10 ml-sm-auto px-4">
    <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
        <h1 class="h2">分类管理</h1>
        <div class="btn-toolbar mb-2 mb-md-0">
            <div class="btn-group mr-2">
                <a href="category_add.jsp" class="btn btn-sm btn-success">
                    <i class="fas fa-plus"></i> 添加分类
                </a>
            </div>
        </div>
    </div>

    <!-- 搜索栏 -->
    <div class="row mb-3">
        <div class="col-md-6">
            <form action="categoryManage" method="get" class="d-flex">
                <input type="text" name="keyword" class="form-control search-input"
                       placeholder="搜索分类名称..." value="${param.keyword}">
                <button type="submit" class="btn btn-primary search-btn">
                    <i class="fas fa-search"></i> 搜索
                </button>
            </form>
        </div>
    </div>

    <!-- 分类列表 -->
    <div class="table-responsive">
        <table class="table table-hover">
            <thead class="table-light">
                <tr>
                    <th>编号</th>
                    <th>ID</th>
                    <th>分类名称</th>
                    <th>新闻数量</th>
                    <th>类型</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${categoryList}" var="category" varStatus="status">
                    <tr>
                        <td>${(currentPage-1) * 10 + status.count}</td>
                        <td>${category.sort_id}</td>
                        <td>
                            <span class="category-name">${category.name}</span>
                        </td>
                        <td>
                            <span class="news-count">
                                <i class="fas fa-newspaper"></i>
                                ${category.newsCount} 篇
                            </span>
                        </td>
                        <td>
                            <span class="category-badge ${category.sort_id <= 8 ? 'category-default' : 'category-custom'}">
                                ${category.sort_id <= 8 ? '默认分类' : '自定义分类'}
                            </span>
                        </td>
                        <td>
                            <div class="action-buttons">
                                <a href="categoryEdit?id=${category.sort_id}" 
                                   class="btn btn-sm btn-info"
                                   ${category.sort_id <= 8 ? 'disabled' : ''}>
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                <button onclick="deleteCategory(${category.sort_id})" 
                                        class="btn btn-sm btn-danger"
                                        ${category.sort_id <= 8 ? 'disabled' : ''}>
                                    <i class="fas fa-trash"></i> 删除
                                </button>
                            </div>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </div>

    <!-- 分页 -->
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            <li class="page-item ${currentPage == 1 ? 'disabled' : ''}">
                <a class="page-link" href="categoryManage?page=${currentPage-1}&keyword=${param.keyword}">上一页</a>
            </li>
            <c:forEach begin="1" end="${totalPages}" var="i">
                <li class="page-item ${currentPage == i ? 'active' : ''}">
                    <a class="page-link" href="categoryManage?page=${i}&keyword=${param.keyword}">${i}</a>
                </li>
            </c:forEach>
            <li class="page-item ${currentPage == totalPages ? 'disabled' : ''}">
                <a class="page-link" href="categoryManage?page=${currentPage+1}&keyword=${param.keyword}">下一页</a>
            </li>
        </ul>
    </nav>
</main>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                确定要删除这个分类吗？此操作不可恢复。
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="button" class="btn btn-danger" id="confirmDelete">确定删除</button>
            </div>
        </div>
    </div>
</div>

<script>
    let categoryIdToDelete = null;

    function deleteCategory(categoryId) {
        categoryIdToDelete = categoryId;
        $('#deleteModal').modal('show');
    }

    $('#confirmDelete').click(function() {
        if (categoryIdToDelete) {
            window.location.href = 'categoryDelete?id=' + categoryIdToDelete;
        }
        $('#deleteModal').modal('hide');
    });
</script>
</body>
</html> 